from backend.app.app.db.session_public import session_pub
import backend.app.app.models.db_owi_public_models as md_pub


# 单元基本信息 读库
def read_unit_basic_data():
    try:
        return_data = session_pub.query(
            md_pub.TbDvpunitbaisc
        ).all()
        print(return_data)
        return return_data
    except Exception as e:
        print(e)
        print(e.__traceback__.tb_lineno)
        return False


# 单元基本信息 存库
def save_unit_basic_data(save, modify, delete):

    return_data = {
        "save": [],
        "modify": [],
        "delete": []
    }
    # save
    for data in save:
        data = data.__dict__
        temp_dict = {
            "DvpUnit_ID": data['DvpUnit_ID'],
            "DvpUnit_Name": data['DvpUnit_Name'],
            "result": True
        }
        try:
            insert_data = md_pub.TbDvpunitbaisc(
                DvpUnit_ID=data["DvpUnit_ID"],
                DvpUnit_Name=data["DvpUnit_Name"],
                Belong_DvpUnit_Name=data["Belong_DvpUnit_Name"],
                DvpUnit_Rank=data["DvpUnit_Rank"],
                Remark=data["Remark"]
            )
            session_pub.add(insert_data)
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["save"].append(temp_dict)
    # modify
    for data in modify:
        data = data.__dict__
        temp_dict = {
            "DvpUnit_ID": data['DvpUnit_ID'],
            "DvpUnit_Name": data['DvpUnit_Name'],
            "result": True
        }
        try:
            session_pub.query(
                md_pub.TbDvpunitbaisc
            ).filter(
                md_pub.TbDvpunitbaisc.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbDvpunitbaisc.DvpUnit_ID == data['DvpUnit_ID']
            ).update({
                md_pub.TbDvpunitbaisc.DvpUnit_Rank: data['DvpUnit_Rank'],
                md_pub.TbDvpunitbaisc.Belong_DvpUnit_Name: data['Belong_DvpUnit_Name'],
                md_pub.TbDvpunitbaisc.Remark: data['Remark']
            })
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["modify"].append(temp_dict)
    # delete
    for data in delete:
        data = data.__dict__
        temp_dict = {
            "DvpUnit_ID": data['DvpUnit_ID'],
            "DvpUnit_Name": data['DvpUnit_Name'],
            "result": True
        }
        try:
            session_pub.query(
                md_pub.TbDvpunitbaisc
            ).filter(
                md_pub.TbDvpunitbaisc.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbDvpunitbaisc.DvpUnit_ID == data['DvpUnit_ID']
            ).delete()
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["delete"].append(temp_dict)
    return return_data


# 单井基本信息 读库
def read_well_basic_data():
    try:
        return_data = session_pub.query(
            md_pub.TbWellbasic
        ).all()
        print(return_data)
        return return_data
    except Exception as e:
        print(e)
        print(e.__traceback__.tb_lineno)
        return False


# 单井基本信息 存库
def save_well_basic_data(save, modify, delete):
    return_data = {
        "save": [],
        "modify": [],
        "delete": []
    }
    # save
    for data in save:
        data = data.__dict__
        temp_dict = {
            "Well_ID": data['Well_ID'],
            "Well_Name": data['Well_Name'],
            "result": True
        }
        try:
            insert_data = md_pub.TbWellbasic(
                Well_ID=data['Well_ID'],
                Well_Name=data["Well_Name"],
                GeoCoordX=data["GeoCoordX"],
                GeoCoordY=data["GeoCoordX"],
                Belong_DvpUnit_Name=data["Belong_DvpUnit_Name"],
                Remark=data["Remark"]
            )
            session_pub.add(insert_data)
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["save"].append(temp_dict)
    # modify
    for data in modify:
        data = data.__dict__
        temp_dict = {
            "Well_ID": data['Well_ID'],
            "Well_Name": data['Well_Name'],
            "result": True
        }
        try:
            session_pub.query(
                md_pub.TbWellbasic
            ).filter(
                md_pub.TbWellbasic.Well_ID == data['Well_ID'],
                md_pub.TbWellbasic.Well_Name == data['Well_Name']
            ).update({
                md_pub.TbWellbasic.GeoCoordX: data['GeoCoordX'],
                md_pub.TbWellbasic.GeoCoordY: data['GeoCoordY'],
                md_pub.TbWellbasic.Belong_DvpUnit_Name: data['Belong_DvpUnit_Name'],
                md_pub.TbWellbasic.Remark: data['Remark']
            })
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["modify"].append(temp_dict)
    # delete
    for data in delete:
        data = data.__dict__
        temp_dict = {
            "Well_ID": data['Well_ID'],
            "Well_Name": data['Well_Name'],
            "result": True
        }
        try:
            session_pub.query(
                md_pub.TbWellbasic
            ).filter(
                md_pub.TbWellbasic.Well_Name == data['Well_Name'],
                md_pub.TbWellbasic.Well_ID == data['Well_ID']
            ).delete()
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["delete"].append(temp_dict)
    return return_data


# 措施分类信息 读库
def read_oil_well_stimulate_data():
    try:
        return_data = session_pub.query(
            md_pub.TbOilwellstimType
        ).all()
        print(return_data)
        return return_data
    except Exception as e:
        print(e)
        print(e.__traceback__.tb_lineno)
        return False


# 措施分类信息 存库
def save_oil_well_stimulate_data(save, modify, delete):
    return_data = {
        "save": [],
        "modify": [],
        "delete": []
    }
    # save
    for data in save:
        data = data.__dict__
        temp_dict = {
                "StimType_ID": data['StimType_ID'],
                "StimType_Name": data['StimType_Name'],
                "result": True
            }
        try:
            insert_data = md_pub.TbOilwellstimType(
                StimType_ID=data["StimType_ID"],
                StimType_Name=data["StimType_Name"],
                Belong_StimType_Name=data["Belong_StimType_Name"],
                StimType_Rank=data["StimType_Rank"],
                Remark=data["Remark"],
            )
            session_pub.add(insert_data)
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["save"].append(temp_dict)
    # modify
    for data in modify:
        data = data.__dict__
        temp_dict = {
                "StimType_ID": data['StimType_ID'],
                "StimType_Name": data['StimType_Name'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbOilwellstimType
            ).filter(
                md_pub.TbOilwellstimType.StimType_ID == data['StimType_ID'],
                md_pub.TbOilwellstimType.StimType_Name == data['StimType_Name']
            ).update({
                md_pub.TbOilwellstimType.Belong_StimType_Name: data['Belong_StimType_Name'],
                md_pub.TbOilwellstimType.Remark: data['Remark'],
                md_pub.TbOilwellstimType.StimType_Rank: data['StimType_Rank']
            })
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["modify"].append(temp_dict)
    # delete
    for data in delete:
        data = data.__dict__
        temp_dict = {
                "StimType_ID": data['StimType_ID'],
                "StimType_Name": data['StimType_Name'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbOilwellstimType
            ).filter(
                md_pub.TbOilwellstimType.StimType_ID == data['StimType_ID'],
                md_pub.TbOilwellstimType.StimType_Name == data['StimType_Name']
            ).delete()
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["delete"].append(temp_dict)
    return return_data


# 单元措施效果 读库
def read_unit_stimulate_result_data():
    try:
        return_data = session_pub.query(
            md_pub.TbYearDvpunitOilstimResult
        ).all()
        print(return_data)
        return return_data
    except Exception as e:
        print(e)
        print(e.__traceback__.tb_lineno)
        return False


# 单元措施效果 存库
def save_unit_stimulate_result_data(save, modify, delete):
    return_data = {
        "save": [],
        "modify": [],
        "delete": []
    }
    # save
    for data in save:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "StimType_Name": data['StimType_Name'],
                "result": True
            }
        try:
            insert_data = md_pub.TbYearDvpunitOilstimResult(
                DvpUnit_Name=data["DvpUnit_Name"],
                Year=data["Year"],
                StimType_Name=data["StimType_Name"],
                Stim_WellNumber_Valid=data["Stim_WellNumber_Valid"],
                Stim_WellValidRatio=data["Stim_WellValidRatio"],
                Stim_ValidPeriod_Average=data["Stim_ValidPeriod_Average"],
                Stim_IncreOil_Average=data["Stim_IncreOil_Average"],
                Remark=data["Remark"],
            )
            session_pub.add(insert_data)
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["save"].append(temp_dict)
    # modify
    for data in modify:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "StimType_Name": data['StimType_Name'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbYearDvpunitOilstimResult
            ).filter(
                md_pub.TbYearDvpunitOilstimResult.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbYearDvpunitOilstimResult.Year == data['Year'],
                md_pub.TbYearDvpunitOilstimResult.StimType_Name == data['StimType_Name']
            ).update({
                md_pub.TbYearDvpunitOilstimResult.Stim_WellNumber_Valid: data['Stim_WellNumber_Valid'],
                md_pub.TbYearDvpunitOilstimResult.Stim_WellValidRatio: data['Stim_WellValidRatio'],
                md_pub.TbYearDvpunitOilstimResult.Stim_ValidPeriod_Average: data['Stim_ValidPeriod_Average'],
                md_pub.TbYearDvpunitOilstimResult.Stim_IncreOil_Average: data['Stim_IncreOil_Average'],
                md_pub.TbYearDvpunitOilstimResult.Remark: data['Remark']
            })
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["modify"].append(temp_dict)
    # delete
    for data in delete:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "StimType_Name": data['StimType_Name'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbYearDvpunitOilstimResult
            ).filter(
                md_pub.TbYearDvpunitOilstimResult.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbYearDvpunitOilstimResult.Year == data['Year'],
                md_pub.TbYearDvpunitOilstimResult.StimType_Name == data['StimType_Name']
            ).delete()
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["delete"].append(temp_dict)
    return return_data


# 单井措施效果 读库
def read_well_stimulate_result_data():
    try:
        return_data = session_pub.query(
            md_pub.TbOilwellstimResult
        ).all()
        print(return_data)
        return return_data
    except Exception as e:
        print(e)
        print(e.__traceback__.tb_lineno)
        return False


# 单井措施效果 存库
def save_well_stimulate_result_data(save, modify, delete):

    return_data = {
        "save": [],
        "modify": [],
        "delete": []
    }
    # save
    for data in save:
        data = data.__dict__
        temp_dict = {
                "Well_Name": data['Well_Name'],
                "Stim_Date": data['Stim_Date'],
                "StimType_Name": data['StimType_Name'],
                "result": True
            }
        try:
            insert_data = md_pub.TbOilwellstimResult(
                Well_Name=data["Well_Name"],
                Stim_Date=data["Stim_Date"],
                StimType_Name=data["StimType_Name"],
                Stim_ValidPeriod=data["Stim_ValidPeriod"],
                Stim_IncreOil=data["Stim_IncreOil"],
                Remark=data["Remark"],
            )
            session_pub.add(insert_data)
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["save"].append(temp_dict)
    # modify
    for data in modify:
        data = data.__dict__
        temp_dict = {
                "Well_Name": data['Well_Name'],
                "Stim_Date": data['Stim_Date'],
                "StimType_Name": data['StimType_Name'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbOilwellstimResult
            ).filter(
                md_pub.TbOilwellstimResult.Well_Name == data['Well_Name'],
                md_pub.TbOilwellstimResult.Stim_Date == data['Stim_Date'],
                md_pub.TbOilwellstimResult.StimType_Name == data['StimType_Name']
            ).update({
                md_pub.TbOilwellstimResult.Stim_ValidPeriod: data['Stim_ValidPeriod'],
                md_pub.TbOilwellstimResult.Stim_IncreOil: data['Stim_IncreOil'],
                md_pub.TbOilwellstimResult.Remark: data['Remark']
            })
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["modify"].append(temp_dict)
    # delete
    for data in delete:
        data = data.__dict__
        temp_dict = {
                "Well_Name": data['Well_Name'],
                "Stim_Date": data['Stim_Date'],
                "StimType_Name": data['StimType_Name'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbOilwellstimResult
            ).filter(
                md_pub.TbOilwellstimResult.Well_Name == data['Well_Name'],
                md_pub.TbOilwellstimResult.Stim_Date == data['Stim_Date'],
                md_pub.TbOilwellstimResult.StimType_Name == data['StimType_Name']
            ).delete()
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["delete"].append(temp_dict)
    return return_data


# 措施作业费用 读库
def read_stimulate_cost_data():
    try:
        return_data = session_pub.query(
            md_pub.TbYearDvpunitOilstimCost
        ).all()
        print(return_data)
        return return_data
    except Exception as e:
        print(e)
        print(e.__traceback__.tb_lineno)
        return False


# 措施作业费用 存库
def save_stimulate_cost_data(save, modify, delete):
    return_data = {
        "save": [],
        "modify": [],
        "delete": []
    }
    # save
    for data in save:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "StimType_Name": data['StimType_Name'],
                "result": True
            }
        try:
            insert_data = md_pub.TbYearDvpunitOilstimCost(
                DvpUnit_Name=data["DvpUnit_Name"],
                Year=data["Year"],
                StimType_Name=data["StimType_Name"],
                Stim_Cost=data["Stim_Cost"],
                Remark=data["Remark"],
            )
            session_pub.add(insert_data)
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["save"].append(temp_dict)
    # modify
    for data in modify:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "StimType_Name": data['StimType_Name'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbYearDvpunitOilstimCost
            ).filter(
                md_pub.TbYearDvpunitOilstimCost.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbYearDvpunitOilstimCost.Year == data['Year'],
                md_pub.TbYearDvpunitOilstimCost.StimType_Name == data['StimType_Name']
            ).update({
                md_pub.TbYearDvpunitOilstimCost.Stim_Cost: data['Stim_Cost'],
                md_pub.TbYearDvpunitOilstimCost.Remark: data['Remark']
            })
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["modify"].append(temp_dict)
    # delete
    for data in delete:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "StimType_Name": data['StimType_Name'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbYearDvpunitOilstimCost
            ).filter(
                md_pub.TbYearDvpunitOilstimCost.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbYearDvpunitOilstimCost.Year == data['Year'],
                md_pub.TbYearDvpunitOilstimCost.StimType_Name == data['StimType_Name']
            ).delete()
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["delete"].append(temp_dict)
    return return_data


# 单元原油价格 读库
def read_unit_oil_price_data():
    try:
        return_data = session_pub.query(
            md_pub.TbYearDvpunitOilprice
        ).all()
        print(return_data)
        return return_data
    except Exception as e:
        print(e)
        print(e.__traceback__.tb_lineno)
        return False


# 单元原油价格 存库
def save_unit_oil_price_data(save, modify, delete):
    return_data = {
        "save": [],
        "modify": [],
        "delete": []
    }
    # save
    for data in save:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "result": True
            }
        try:
            insert_data = md_pub.TbYearDvpunitOilprice(
                DvpUnit_Name=data["DvpUnit_Name"],
                Year=data["Year"],
                Oil_CommodityRatio=data["Oil_CommodityRatio"],
                Oil_Price=data["Oil_Price"],
                Remark=data["Remark"],
            )
            session_pub.add(insert_data)
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["save"].append(temp_dict)
    # modify
    for data in modify:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbYearDvpunitOilprice
            ).filter(
                md_pub.TbYearDvpunitOilprice.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbYearDvpunitOilprice.Year == data['Year'],
            ).update({
                md_pub.TbYearDvpunitOilprice.Oil_CommodityRatio: data['Oil_CommodityRatio'],
                md_pub.TbYearDvpunitOilprice.Oil_Price: data['Oil_Price'],
                md_pub.TbYearDvpunitOilprice.Remark: data['Remark']
            })
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["modify"].append(temp_dict)
    # delete
    for data in delete:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbYearDvpunitOilprice
            ).filter(
                md_pub.TbYearDvpunitOilprice.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbYearDvpunitOilprice.Year == data['Year'],
            ).delete()
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["delete"].append(temp_dict)
    return return_data


# 增油操作成本 读库
def read_oil_operate_cost_data():
    try:
        return_data = session_pub.query(
            md_pub.TbYearDvpunitOilprodcost
        ).all()
        print(return_data)
        return return_data
    except Exception as e:
        print(e)
        print(e.__traceback__.tb_lineno)
        return False


# 增油操作成本 存库
def save_oil_operate_cost_data(save, modify, delete):
    return_data = {
        "save": [],
        "modify": [],
        "delete": []
    }
    # save
    for data in save:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "result": True
            }
        try:
            insert_data = md_pub.TbYearDvpunitOilprodcost(
                DvpUnit_Name=data["DvpUnit_Name"],
                Year=data["Year"],
                Oil_OprCost=data["Oil_OprCost"],
                FuelCost=data["FuelCost"],
                Power_Cost=data["Power_Cost"],
                FluidDispose_Cost=data["FluidDispose_Cost"],
                OilDisplaceAgent_Cost=data["OilDisplaceAgent_Cost"],
                NGLRecovery_Cost=data["NGLRecovery_Cost"],
                Transp_Cost=data["Transp_Cost"],
                WellTest_Cost=data["WellTest_Cost"],
                Other_Cost=data["Other_Cost"],
                Depre_Cost=data["Depre_Cost"],
                Remark=data["Remark"],
            )
            session_pub.add(insert_data)
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["save"].append(temp_dict)
    # modify
    for data in modify:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbYearDvpunitOilprodcost
            ).filter(
                md_pub.TbYearDvpunitOilprodcost.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbYearDvpunitOilprodcost.Year == data['Year'],
            ).update({
                md_pub.TbYearDvpunitOilprodcost.Oil_OprCost: data['Oil_OprCost'],
                md_pub.TbYearDvpunitOilprodcost.FuelCost: data['FuelCost'],
                md_pub.TbYearDvpunitOilprodcost.Power_Cost: data['Power_Cost'],
                md_pub.TbYearDvpunitOilprodcost.FluidDispose_Cost: data['FluidDispose_Cost'],
                md_pub.TbYearDvpunitOilprodcost.OilDisplaceAgent_Cost: data['OilDisplaceAgent_Cost'],
                md_pub.TbYearDvpunitOilprodcost.NGLRecovery_Cost: data['NGLRecovery_Cost'],
                md_pub.TbYearDvpunitOilprodcost.Transp_Cost: data['Transp_Cost'],
                md_pub.TbYearDvpunitOilprodcost.WellTest_Cost: data['WellTest_Cost'],
                md_pub.TbYearDvpunitOilprodcost.Other_Cost: data['Other_Cost'],
                md_pub.TbYearDvpunitOilprodcost.Depre_Cost: data['Depre_Cost'],
                md_pub.TbYearDvpunitOilprodcost.Remark: data['Remark']
            })
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["modify"].append(temp_dict)
    # delete
    for data in delete:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbYearDvpunitOilprodcost
            ).filter(
                md_pub.TbYearDvpunitOilprodcost.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbYearDvpunitOilprodcost.Year == data['Year'],
            ).delete()
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["delete"].append(temp_dict)
    return return_data


# 增油销售成本 读库
def read_oil_sale_cost_data():
    try:
        return_data = session_pub.query(
            md_pub.TbYearDvpunitOilsalecostRatio
        ).all()
        print(return_data)
        return return_data
    except Exception as e:
        print(e)
        print(e.__traceback__.tb_lineno)
        return False


# 增油销售成本 存库
def save_oil_sale_cost_data(save, modify, delete):
    return_data = {
        "save": [],
        "modify": [],
        "delete": []
    }
    # save
    for data in save:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "result": True
            }
        try:
            insert_data = md_pub.TbYearDvpunitOilsalecostRatio(
                DvpUnit_Name=data["DvpUnit_Name"],
                Year=data["Year"],
                OilSale_TotalTaxRatio=data["OilSale_TotalTaxRatio"],
                AddedValue_TaxRatio=data["AddedValue_TaxRatio"],
                Resource_TaxRatio=data["Resource_TaxRatio"],
                EduAdd_TaxRatio=data["EduAdd_TaxRatio"],
                CityDvpAdd_TaxRate=data["CityDvpAdd_TaxRate"],
                Income_TaxRate=data["Income_TaxRate"],
                ManageCost_Ratio=data["ManageCost_Ratio"],
                Remark=data["Remark"]
            )
            session_pub.add(insert_data)
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["save"].append(temp_dict)
    # modify
    for data in modify:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbYearDvpunitOilsalecostRatio
            ).filter(
                md_pub.TbYearDvpunitOilsalecostRatio.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbYearDvpunitOilsalecostRatio.Year == data['Year'],
            ).update({
                md_pub.TbYearDvpunitOilsalecostRatio.OilSale_TotalTaxRatio: data['OilSale_TotalTaxRatio'],
                md_pub.TbYearDvpunitOilsalecostRatio.AddedValue_TaxRatio: data['AddedValue_TaxRatio'],
                md_pub.TbYearDvpunitOilsalecostRatio.Resource_TaxRatio: data['Resource_TaxRatio'],
                md_pub.TbYearDvpunitOilsalecostRatio.EduAdd_TaxRatio: data['EduAdd_TaxRatio'],
                md_pub.TbYearDvpunitOilsalecostRatio.CityDvpAdd_TaxRate: data['CityDvpAdd_TaxRate'],
                md_pub.TbYearDvpunitOilsalecostRatio.Income_TaxRate: data['Income_TaxRate'],
                md_pub.TbYearDvpunitOilsalecostRatio.ManageCost_Ratio: data['ManageCost_Ratio'],
                md_pub.TbYearDvpunitOilsalecostRatio.Remark: data['Remark']
            })
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["modify"].append(temp_dict)
    # delete
    for data in delete:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbYearDvpunitOilsalecostRatio
            ).filter(
                md_pub.TbYearDvpunitOilsalecostRatio.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbYearDvpunitOilsalecostRatio.Year == data['Year'],
            ).delete()
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["delete"].append(temp_dict)
    return return_data


# 资金成本 读库
def read_cost_data():
    try:
        return_data = session_pub.query(
            md_pub.TbYearDvpunitDiscount
        ).all()
        print(return_data)
        return return_data
    except Exception as e:
        print(e)
        print(e.__traceback__.tb_lineno)
        return False


# 资金成本 存库
def save_cost_data(save, modify, delete):
    return_data = {
        "save": [],
        "modify": [],
        "delete": []
    }
    # save
    for data in save:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "result": True
            }
        try:
            insert_data = md_pub.TbYearDvpunitDiscount(
                DvpUnit_Name=data["DvpUnit_Name"],
                Year=data["Year"],
                Money_Discount=data["Money_Discount"],
                Remark=data["Remark"],
            )
            session_pub.add(insert_data)
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["save"].append(temp_dict)
    # modify
    for data in modify:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbYearDvpunitDiscount
            ).filter(
                md_pub.TbYearDvpunitDiscount.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbYearDvpunitDiscount.Year == data['Year'],
            ).update({
                md_pub.TbYearDvpunitDiscount.Money_Discount: data['Money_Discount'],
                md_pub.TbYearDvpunitDiscount.Remark: data['Remark'],
            })
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["modify"].append(temp_dict)
    # delete
    for data in delete:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbYearDvpunitDiscount
            ).filter(
                md_pub.TbYearDvpunitDiscount.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbYearDvpunitDiscount.Year == data['Year'],
            ).delete()
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["delete"].append(temp_dict)
    return return_data


# 单元经济评价结果 读库
def read_unit_stimulate_evaluate_result_data():
    try:
        return_data = session_pub.query(
            md_pub.TbYearDvpunitOilstimulateEvaluateResult
        ).all()
        print(return_data)
        return return_data
    except Exception as e:
        print(e)
        print(e.__traceback__.tb_lineno)
        return False


# 单元经济评价结果 存库
def save_unit_stimulate_evaluate_result_data(save, modify, delete):

    return_data = {
        "save": [],
        "modify": [],
        "delete": []
    }
    # save
    for data in save:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "result": True
            }
        try:
            insert_data = md_pub.TbYearDvpunitOilstimulateEvaluateResult(
                DvpUnit_Name=data["DvpUnit_Name"],
                Year=data["Year"],
                StimType_Name=data["StimType_Name"],
                Stim_WellValidRatio=data["Stim_WellValidRatio"],
                Stim_WellNumber_Valid=data["Stim_WellNumber_Valid"],
                Stim_ValidPeriod_Average=data["Stim_ValidPeriod_Average"],
                Stim_IncreOil_Average=data["Stim_IncreOil_Average"],
                StimCost_Average=data["StimCost_Average"],
                StimOil_OprCost_Average=data["StimOil_OprCost_Average"],
                StimOil_CommodityRatio=data["StimOil_CommodityRatio"],
                StimOil_Gross_SalesIncome_Average=data["StimOil_Gross_SalesIncome_Average"],
                StimOil_Total_TaxRatio_Average=data["StimOil_Total_TaxRatio_Average"],
                StimOil_Static_Gains_Average=data["StimOil_Static_Gains_Average"],
                StimOil_NPV_Average=data["StimOil_NPV_Average"],
                StimOil_ROI=data["StimOil_ROI"],
                StimOil_NPVR=data["StimOil_NPVR"],
                StimOil_FIRR=data["StimOil_FIRR"],
                Stim_IncreOil_Critical_Average=data["Stim_IncreOil_Critical_Average"],
                StimOil_Price_Critical_Average=data["StimOil_Price_Critical_Average"],
                Oil_Price=data["Oil_Price"],
                OilSale_TotalTaxRatio=data["OilSale_TotalTaxRatio"],
                Money_Discount=data["Money_Discount"],
                Oil_OprCost=data["Oil_OprCost"],
                StimOil_Price_Critical=data["StimOil_Price_Critical"],
                StimOil_WellOprCost_Average=data["StimOil_WellOprCost_Average"],
                Remark=data["Remark"],
            )
            session_pub.add(insert_data)
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["save"].append(temp_dict)
    # modify
    for data in modify:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbYearDvpunitOilstimulateEvaluateResult
            ).filter(
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Year == data['Year'],
            ).update({
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimType_Name: data['StimType_Name'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_WellValidRatio: data['Stim_WellValidRatio'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_WellNumber_Valid: data['Stim_WellNumber_Valid'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_ValidPeriod_Average: data['Stim_ValidPeriod_Average'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_IncreOil_Average: data['Stim_IncreOil_Average'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimCost_Average: data['StimCost_Average'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_OprCost_Average: data['StimOil_OprCost_Average'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_CommodityRatio: data['StimOil_CommodityRatio'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Gross_SalesIncome_Average: data['StimOil_Gross_SalesIncome_Average'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Total_TaxRatio_Average: data['StimOil_Total_TaxRatio_Average'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Static_Gains_Average: data['StimOil_Static_Gains_Average'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_NPV_Average: data['StimOil_NPV_Average'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_ROI: data['StimOil_ROI'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_NPVR: data['StimOil_NPVR'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_FIRR: data['StimOil_FIRR'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Stim_IncreOil_Critical_Average: data['Stim_IncreOil_Critical_Average'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Price_Critical_Average: data['StimOil_Price_Critical_Average'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Oil_Price: data['Oil_Price'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.OilSale_TotalTaxRatio: data['OilSale_TotalTaxRatio'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Money_Discount: data['Money_Discount'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Oil_OprCost: data['Oil_OprCost'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_Price_Critical: data['StimOil_Price_Critical'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.StimOil_WellOprCost_Average: data['StimOil_WellOprCost_Average'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Remark: data['Remark'],
            })
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["modify"].append(temp_dict)
    # delete
    for data in delete:
        data = data.__dict__
        temp_dict = {
                "DvpUnit_Name": data['DvpUnit_Name'],
                "Year": data['Year'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.TbYearDvpunitOilstimulateEvaluateResult
            ).filter(
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.DvpUnit_Name == data['DvpUnit_Name'],
                md_pub.TbYearDvpunitOilstimulateEvaluateResult.Year == data['Year'],
            ).delete()
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["delete"].append(temp_dict)
    return return_data


# 单井经济评价结果 读库
def read_well_stimulate_evaluate_result_data():
    try:
        return_data = session_pub.query(
            md_pub.Tb_OilWellStimEvaluateResult
        ).all()
        print(return_data)
        return return_data
    except Exception as e:
        print(e)
        print(e.__traceback__.tb_lineno)
        return False


# 单井经济评价结果 存库
def save_well_stimulate_evaluate_result_data(save, modify, delete):

    return_data = {
        "save": [],
        "modify": [],
        "delete": []
    }
    # save
    for data in save:
        data = data.__dict__
        temp_dict = {
                "Well_Name": data['Well_Name'],
                "Stim_Date": data['Stim_Date'],
                "StimType_Name": data['StimType_Name'],
                "result": True
            }
        try:
            insert_data = md_pub.Tb_OilWellStimEvaluateResult(
                Well_Name=data["Well_Name"],
                Stim_Date=data["Stim_Date"],
                StimType_Name=data["StimType_Name"],
                Stim_ValidPeriod=data["Stim_ValidPeriod"],
                Stim_IncreOil=data["Stim_IncreOil"],
                StimCost=data["StimCost"],
                StimOil_WellOprCost=data["StimOil_WellOprCost"],
                StimOil_CommodityRatio=data["StimOil_CommodityRatio"],
                StimOil_Gross_SalesIncome=data["StimOil_Gross_SalesIncome"],
                StimOil_Total_Tax=data["StimOil_Total_Tax"],
                StimOil_Static_Gains=data["StimOil_Static_Gains"],
                StimOil_NPV=data["StimOil_NPV"],
                StimOil_ROI=data["StimOil_ROI"],
                StimOil_NPVR=data["StimOil_NPVR"],
                StimOil_FIRR=data["StimOil_FIRR"],
                Stim_IncreOil_Critical=data["Stim_IncreOil_Critical"],
                StimOil_Price_Critical=data["StimOil_Price_Critical"],
                Oil_Price=data["Oil_Price"],
                OilSale_TotalTaxRatio=data["OilSale_TotalTaxRatio"],
                Money_Discount=data["Money_Discount"],
                Oil_OprCost=data["Oil_OprCost"],
                Remark=data["Remark"],
            )
            session_pub.add(insert_data)
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["save"].append(temp_dict)
    # modify
    for data in modify:
        data = data.__dict__
        temp_dict = {
                "Well_Name": data['Well_Name'],
                "Stim_Date": data['Stim_Date'],
                "StimType_Name": data['StimType_Name'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.Tb_OilWellStimEvaluateResult
            ).filter(
                md_pub.Tb_OilWellStimEvaluateResult.Well_Name == data['Well_Name'],
                md_pub.Tb_OilWellStimEvaluateResult.Stim_Date == data['Stim_Date'],
                md_pub.Tb_OilWellStimEvaluateResult.StimType_Name == data['StimType_Name']
            ).update({
                md_pub.Tb_OilWellStimEvaluateResult.Stim_ValidPeriod: data['Stim_ValidPeriod'],
                md_pub.Tb_OilWellStimEvaluateResult.Stim_IncreOil: data['Stim_IncreOil'],
                md_pub.Tb_OilWellStimEvaluateResult.StimCost: data['StimCost'],
                md_pub.Tb_OilWellStimEvaluateResult.StimOil_WellOprCost: data['StimOil_WellOprCost'],
                md_pub.Tb_OilWellStimEvaluateResult.StimOil_CommodityRatio: data['StimOil_CommodityRatio'],
                md_pub.Tb_OilWellStimEvaluateResult.StimOil_Gross_SalesIncome: data['StimOil_Gross_SalesIncome'],
                md_pub.Tb_OilWellStimEvaluateResult.StimOil_Total_Tax: data['StimOil_Total_Tax'],
                md_pub.Tb_OilWellStimEvaluateResult.StimOil_Static_Gains: data['StimOil_Static_Gains'],
                md_pub.Tb_OilWellStimEvaluateResult.StimOil_NPV: data['StimOil_NPV'],
                md_pub.Tb_OilWellStimEvaluateResult.StimOil_ROI: data['StimOil_ROI'],
                md_pub.Tb_OilWellStimEvaluateResult.StimOil_NPVR: data['StimOil_NPVR'],
                md_pub.Tb_OilWellStimEvaluateResult.StimOil_FIRR: data['StimOil_FIRR'],
                md_pub.Tb_OilWellStimEvaluateResult.Stim_IncreOil_Critical: data['Stim_IncreOil_Critical'],
                md_pub.Tb_OilWellStimEvaluateResult.StimOil_Price_Critical: data['StimOil_Price_Critical'],
                md_pub.Tb_OilWellStimEvaluateResult.Oil_Price: data['Oil_Price'],
                md_pub.Tb_OilWellStimEvaluateResult.OilSale_TotalTaxRatio: data['OilSale_TotalTaxRatio'],
                md_pub.Tb_OilWellStimEvaluateResult.Money_Discount: data['Money_Discount'],
                md_pub.Tb_OilWellStimEvaluateResult.Oil_OprCost: data['Oil_OprCost'],
                md_pub.Tb_OilWellStimEvaluateResult.Remark: data['Remark']
            })
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["modify"].append(temp_dict)
    # delete
    for data in delete:
        data = data.__dict__
        temp_dict = {
                "Well_Name": data['Well_Name'],
                "Stim_Date": data['Stim_Date'],
                "StimType_Name": data['StimType_Name'],
                "result": True
            }
        try:
            session_pub.query(
                md_pub.Tb_OilWellStimEvaluateResult
            ).filter(
                md_pub.Tb_OilWellStimEvaluateResult.Well_Name == data['Well_Name'],
                md_pub.Tb_OilWellStimEvaluateResult.Stim_Date == data['Stim_Date'],
                md_pub.Tb_OilWellStimEvaluateResult.StimType_Name == data['StimType_Name']
            ).delete()
            session_pub.commit()
        except Exception as e:
            temp_dict.update({"result": False})
            print(e)
            print(e.__traceback__.tb_lineno)
            session_pub.rollback()
        return_data["delete"].append(temp_dict)
    return return_data

